package br.com.exceptiongroup.chatexception.controller;

import java.io.Serializable;

import javax.faces.context.FacesContext;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

public abstract class AbstractManagedBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1840156691824188544L;
	
	protected final FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}
	
	/**
	 * Recupera o User do contexto do Spring Security
	 * @return
	 */
	protected User getUsuarioAutenticado() {
		return (User) getSecurityContext().getAuthentication().getPrincipal();
	}
	
	protected boolean isAutenticado() {
		return getSecurityContext().getAuthentication() != null
				&& getUsuarioAutenticado() != null;
	}
	
	protected SecurityContext getSecurityContext() {
		return SecurityContextHolder.getContext();
	}
}
